import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-08-03
 * Time: 15:59
 */
class Solution {
    public boolean isValid(String s) {
        Stack<Character> list1=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch=s.charAt(i);
            if(ch=='('||ch=='['||ch=='{'){
                list1.push(ch);
            }
            else{
                if(list1.isEmpty()){
                    return false;
                }
                char c=list1.peek();
                if(c=='('&&ch==')'||c=='['&&ch==']'||c=='{'&&ch=='}'){
                    list1.pop();
                }else{
                    return false;
                }
            }
        }
        if(list1.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
}
